package com.xiang.sql.service;

import java.util.List;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiang.api.Util;
import com.xiang.sql.entity.User;
import com.xiang.sql.entity.Addr;
import com.xiang.sql.mapper.AddrMapper;

/**
 * 收货地址 服务类
 * 
 * @author mac-xiang
 */
@Service
public class AddrService extends ServiceImpl<AddrMapper, Addr> {
  @Resource
  AddrMapper mapper;

  private QueryWrapper<Addr> readWrapper(JSONObject p, User per) {
    QueryWrapper<Addr> ret = Util.getQuery(p, Addr.class);
    if (per != null && !per.per("收货地址查询")) {
      ret.eq("`create`", per.id);
    }
    return ret;
  }

  private List<Addr> getList(JSONObject p, User per) {
    return mapper.selectList(readWrapper(p, per));
  }

  private Page<Addr> getPage(Page<Addr> page, JSONObject p, User per) {
    return mapper.selectPage(page == null ? new Page<Addr>(1, 100) : page, readWrapper(p, per));
  }

  public Addr create(JSONObject p, User per) {
    if (per != null) {
      Addr entity = Util.getEntity(p, Addr.class);
      entity.createTime = null;
      if (Util.isEmpty(entity.create) || !per.per("收货地址创建")) {
        entity.create = per.id;
      }
      mapper.insert(entity);
      return entity;
    }
    throw new RuntimeException("没有权限");
  }

  public int update(JSONObject p, User per) {
    if (per != null) {
      Addr entity = Util.getEntity(p, Addr.class);
      if (Util.isEmpty(entity.id)) {
        throw new RuntimeException("参数错误: 缺少id");
      }
      QueryWrapper<Addr> qw = Util.getQuery(p, Addr.class);
      if (!per.per("收货地址修改")) {
        qw.eq("`create`", per.id).eq("id", entity.id);
        entity.create = null;
        if (entity.def != null && entity.def != 0) {
          mapper.update(new UpdateWrapper<Addr>().eq("`create`", per.id).set("def", 0));
        }
      }
      entity.createTime = Util.time();
      return mapper.update(entity, qw);
    }
    throw new RuntimeException("没有权限");
  }

  public Object read(JSONObject p, User per) {
    Page<Addr> page = Util.getPage(p, Addr.class);
    return page == null ? getList(p, per) : getPage(page, p, per);
  }

  public int delete(JSONObject p, User per) {
    if (per != null) {
      QueryWrapper<Addr> q = Util.getQuery(p, Addr.class);
      if (!per.per("收货地址删除")) {
        q.eq("`create`", per.id);
      }
      return mapper.delete(q);
    }
    throw new RuntimeException("没有权限");
  }

  public long count(JSONObject p, User per) {
    return this.count(readWrapper(p, per));
  }

  public String getRecvAddr(String id, String cid) {
    final String field = "`name`,`phone`,`addr`";
    Addr r = mapper.selectOne(new QueryWrapper<Addr>().select(field).eq("id", id).eq("`create`", cid));
    if (r == null) {
      r = mapper.selectOne(new QueryWrapper<Addr>().select(field)
          .eq("`create`", cid).last("ORDER BY def DESC,create_time DESC limit 1"));
    }
    if (r == null) {
      throw new RuntimeException("请选择收货地址");
    }
    return Util.toJString(r);
  }
}
